package com.plus.utils;

import cn.hutool.crypto.SecureUtil;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

/**
 * @program: spring-boot-encrypt
 * @ClassName SginUtil
 * @description:
 * @author: AI
 * @create: 2023-12-01 15:25
 * @Version 1.0
 **/
public class SiginUtil {

    /**
     * 生成签名
     * @param parameters
     * @param key 商户ID
     * @return
     */
    public static String createSign(SortedMap<String,Object> parameters, String key){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + key);
        String sign = SecureUtil.md5(sb.toString()).toUpperCase();
        return sign;
    }

}
